package py.com.alfa.escriba.gui.rest.services.resttemplate;

import java.io.IOException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;

import py.com.alfa.escriba.gui.config.EscribaConfigApplicationContextProvider;
import py.com.alfa.escriba.gui.dto.GenerateEscrituraDTO;
import py.com.alfa.escriba.gui.dto.admin.ControlesDTO;
import py.com.alfa.escriba.gui.dto.admin.ControlesEscriturasDTO;
import py.com.alfa.escriba.gui.dto.admin.ControlesFormulariosInfoDTO;
import py.com.alfa.escriba.gui.dto.admin.ControlesInfoDTO;
import py.com.alfa.escriba.gui.dto.admin.ControlesseguimientoDTO;
import py.com.alfa.escriba.gui.dto.admin.ControlestextoDTO;
import py.com.alfa.escriba.gui.dto.query.ControlesQueryDTO;
import py.com.alfa.escriba.gui.dto.query.ControlesRequestDTO;
import py.com.alfa.escriba.gui.rest.services.ControlesService;
import py.com.alfa.escriba.gui.services.Services;
import py.com.sif.rest.client.spring.request.ClientRequestBuilder;
@SuppressWarnings({ "unchecked"})
public class ControlesServiceClient implements ControlesService {
	
	private Logger log = LoggerFactory.getLogger(getClass());
	
	public ControlesServiceClient() {}

	@Override
	public List<ControlesDTO> listControles() throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		ResponseEntity<ControlesDTO[]> response = (ResponseEntity<ControlesDTO[]>) request.exchange(LIST_CONTROL, HttpMethod.GET, ControlesDTO[].class);
		log.info("OK - listControles ");
		List<ControlesDTO> result = request.getArrayAsList(response.getBody());
		return result;
	}
	
	@Override
	public List<ControlesInfoDTO> buscarControles(ControlesQueryDTO control) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(control);
		ResponseEntity<ControlesInfoDTO[]> response = (ResponseEntity<ControlesInfoDTO[]>) request.exchange(BUS_CONTROL, HttpMethod.POST, ControlesInfoDTO[].class);
		log.info("OK - buscarControles ");
		List<ControlesInfoDTO> result = request.getArrayAsList(response.getBody());
		return result;
	}

	@Override
	public ControlesInfoDTO createControl(ControlesInfoDTO control)
			throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(control);
		ResponseEntity<ControlesInfoDTO> response = (ResponseEntity<ControlesInfoDTO>) request.exchange(CREA_CONTROL, HttpMethod.POST, ControlesInfoDTO.class);
		log.info("OK - createControl ");
		ControlesInfoDTO result = response.getBody();
		return result;
	}

	@Override
	public ControlesInfoDTO saveControl(ControlesInfoDTO control) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", control.getId().toString());
		request.addBody(control);
		ResponseEntity<ControlesInfoDTO> response = (ResponseEntity<ControlesInfoDTO>) request.exchange(UPD_CONTROL, HttpMethod.PUT, ControlesInfoDTO.class);
		log.info("OK - saveControl ");
		ControlesInfoDTO result = response.getBody();
		return result;
	}

	@Override
	public void deleteControl(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		request.exchange(DEL_CONTROL, HttpMethod.DELETE, null);
		log.info("OK - deleteControl ");

	}

	@Override
	public ControlesInfoDTO showControl(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		ResponseEntity<ControlesInfoDTO> response = (ResponseEntity<ControlesInfoDTO>) request.exchange(GET_CONTROL, HttpMethod.GET, ControlesInfoDTO.class);
		log.info("OK - showControl ");
		ControlesInfoDTO result = response.getBody();
		return result;
	}
	
	@Override
	public List<ControlesRequestDTO> listControlesRequest(ControlesQueryDTO control) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(control);
		ResponseEntity<ControlesRequestDTO[]> response = (ResponseEntity<ControlesRequestDTO[]>) request.exchange(LIST_REQUEST, HttpMethod.POST, ControlesRequestDTO[].class);
		log.info("OK - listControlesRequest ");
		List<ControlesRequestDTO> result = request.getArrayAsList(response.getBody());
		return result;
	}

	@Override
	public List<ControlesFormulariosInfoDTO> getControlesFormulariosByIdControl(Long idControl) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", idControl.toString());
		ResponseEntity<ControlesFormulariosInfoDTO[]> response = (ResponseEntity<ControlesFormulariosInfoDTO[]>) request.exchange(FORMULARIOS_BY_CONTROL, HttpMethod.GET, ControlesFormulariosInfoDTO[].class);
		log.info("OK - getControlesFormulariosByIdControl ");
		List<ControlesFormulariosInfoDTO> result = request.getArrayAsList(response.getBody());
		return result;
	}

	@Override
	public ControlesFormulariosInfoDTO getControlesFormulariosById(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		ResponseEntity<ControlesFormulariosInfoDTO> response = (ResponseEntity<ControlesFormulariosInfoDTO>) request.exchange(MB_FOMULARIOS, HttpMethod.GET, ControlesFormulariosInfoDTO.class);
		log.info("OK - getControlesFormulariosById ");
		ControlesFormulariosInfoDTO result = response.getBody();
		return result;
	}

	@Override
	public ControlesInfoDTO saveControlesFormularios(ControlesInfoDTO control) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", control.getId().toString());
		request.addBody(control);
		ResponseEntity<ControlesInfoDTO> response = (ResponseEntity<ControlesInfoDTO>) request.exchange(MB_FOMULARIOS, HttpMethod.PUT, ControlesInfoDTO.class);
		log.info("OK - saveControl ");
		ControlesInfoDTO result = response.getBody();
		return result;
	}

	@Override
	public List<ControlesseguimientoDTO> listControlesseguimiento()
			throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		ResponseEntity<ControlesseguimientoDTO[]> response = (ResponseEntity<ControlesseguimientoDTO[]>) request.exchange(CONTOLES_SEGUIMIENTO, HttpMethod.GET, ControlesseguimientoDTO[].class);
		log.info("OK - listControlessegumiento ");
		List<ControlesseguimientoDTO> result = request.getArrayAsList(response.getBody());
		return result;
	}
	
	@Override
	public List<ControlesseguimientoDTO> listControlesseguimientoByIdControl(Long id)
			throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		ResponseEntity<ControlesseguimientoDTO[]> response = (ResponseEntity<ControlesseguimientoDTO[]>) request.exchange(GET_CONTOLES_SEGUIMIENTO, HttpMethod.GET, ControlesseguimientoDTO[].class);
		log.info("OK - listControlessegumiento ");
		List<ControlesseguimientoDTO> result = request.getArrayAsList(response.getBody());
		return result;
	}

	@Override
	public ControlesseguimientoDTO showControlesseguimiento(Long id)
			throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		ResponseEntity<ControlesseguimientoDTO> response = (ResponseEntity<ControlesseguimientoDTO>) request.exchange(MB_CONTOLES_SEGUIMIENTO, HttpMethod.GET, ControlesseguimientoDTO.class);
		log.info("OK - showControlSeguimiento ");
		ControlesseguimientoDTO result = response.getBody();
		return result;
	}

	@Override
	public ControlesseguimientoDTO saveControlesseguimiento(
			ControlesseguimientoDTO dto) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", dto.getId().toString());
		request.addBody(dto);
		ResponseEntity<ControlesseguimientoDTO> response = (ResponseEntity<ControlesseguimientoDTO>) request.exchange(MB_CONTOLES_SEGUIMIENTO, HttpMethod.PUT, ControlesseguimientoDTO.class);
		log.info("OK - saveControl ");
		ControlesseguimientoDTO result = response.getBody();
		return result;
	}

	@Override
	public ControlesseguimientoDTO createControlesseguimiento(
			ControlesseguimientoDTO dto) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(dto);
		ResponseEntity<ControlesseguimientoDTO> response = (ResponseEntity<ControlesseguimientoDTO>) request.exchange(CONTOLES_SEGUIMIENTO, HttpMethod.POST, ControlesseguimientoDTO.class);
		log.info("OK - createControlSeguimiento ");
		ControlesseguimientoDTO result = response.getBody();
		return result;
	}

	@Override
	public void deleteControlesseguimiento(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		request.exchange(MB_CONTOLES_SEGUIMIENTO, HttpMethod.DELETE, null);
		log.info("OK - deleteControlsegumiento ");
		
	}

	@Override
	public List<ControlesEscriturasDTO> getControlesEscriturasByIdControl(Long idControl) throws IOException {
		
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", idControl.toString());
		ResponseEntity<ControlesEscriturasDTO[]> response = (ResponseEntity<ControlesEscriturasDTO[]>) request.exchange(GET_CONTROLES_ESCRITURA, HttpMethod.GET, ControlesEscriturasDTO[].class);
		log.info("OK - getControlesEscriturasByIdControl ");
		List<ControlesEscriturasDTO> result = request.getArrayAsList(response.getBody());
		return result;
	}

	@Override
	public ControlesEscriturasDTO getControlesEscriturasById(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		ResponseEntity<ControlesEscriturasDTO> response = (ResponseEntity<ControlesEscriturasDTO>) request.exchange(MB_CONTROLES_ESCRITURA, HttpMethod.GET, ControlesEscriturasDTO.class);
		log.info("OK - getControlesEscriturasById ");
		ControlesEscriturasDTO result = response.getBody();
		return result;
	}

	@Override
	public ControlesInfoDTO saveControlesEscrituras(ControlesInfoDTO control) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", control.getId().toString());
		request.addBody(control);
		ResponseEntity<ControlesInfoDTO> response = (ResponseEntity<ControlesInfoDTO>) request.exchange(MB_CONTROLES_ESCRITURA, HttpMethod.PUT, ControlesInfoDTO.class);
		log.info("OK - saveControlEscritura ");
		ControlesInfoDTO result = response.getBody();
		return result;
	}
	
	@Override
	public ControlestextoDTO getControlesTextoByIdControl(Long idControl) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", idControl.toString());
		ResponseEntity<ControlestextoDTO> response = (ResponseEntity<ControlestextoDTO>) request.exchange(GET_CONTROLES_TEXTO, HttpMethod.GET, ControlestextoDTO.class);
		log.info("OK - getControlesTextoById ");
		ControlestextoDTO result = response.getBody();
		return result;
	}

	@Override
	public ControlestextoDTO getControlesTextoById(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		ResponseEntity<ControlestextoDTO> response = (ResponseEntity<ControlestextoDTO>) request.exchange(MB_CONTROLES_TEXTO, HttpMethod.GET, ControlestextoDTO.class);
		log.info("OK - getControlesTextoById ");
		ControlestextoDTO result = response.getBody();
		return result;
	}

	@Override
	public ControlesInfoDTO saveControlesTexto(ControlesInfoDTO control) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", control.getId().toString());
		request.addBody(control);
		ResponseEntity<ControlesInfoDTO> response = (ResponseEntity<ControlesInfoDTO>) request.exchange(MB_CONTROLES_TEXTO, HttpMethod.PUT, ControlesInfoDTO.class);
		log.info("OK - saveControlEscritura ");
		ControlesInfoDTO result = response.getBody();
		return result;
	}

	@Override
	public GenerateEscrituraDTO generateEscritura(Long idCcontrol, Long idTemplate) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("idCtrl", idCcontrol.toString());
		request.addParam("idTemplate", idTemplate.toString());
		ResponseEntity<GenerateEscrituraDTO> response = (ResponseEntity<GenerateEscrituraDTO>) request.exchange(GENERATE_ESCRITURA, HttpMethod.GET, GenerateEscrituraDTO.class);
		log.info("OK - generateEscritura ");
		GenerateEscrituraDTO result = response.getBody();
		return result;
	}
	
}
